import StorageManager from "./storageManager";

type path = String | "click"
/**
 * 声音管理
 */
class SoundsManager {

    //背景音量
    private _volumeBGM: number = 1;
    private set volumeBGM(value: number) {
        if (value < 0) {
            this._volumeBGM = 0;
        } else if (value > 1) {
            this._volumeBGM = 1;
        } else {
            this._volumeBGM = value
        }
    }
    private get volumeBGM() {
        return this._volumeBGM
    }

    //音效音量
    private _volumeEffect: number = 1;
    private set volumeEffect(value: number) {
        if (value < 0) {
            this._volumeEffect = 0;
        } else if (value > 1) {
            this._volumeEffect = 1;
        } else {
            this._volumeEffect = value
        }
    }
    private get volumeEffect() {
        return this._volumeEffect
    }

    //音效静音
    private _isEffectMute: boolean = false
    set isEffectMute(value: boolean) {
        this._isEffectMute = value
        StorageManager.saveData("音效静音", value);
    }
    get isEffectMute() {
        StorageManager.getData("音效静音", data => {
            return (this.isEffectMute = data)
        }, () => {
            this.isEffectMute = false
            return this._isEffectMute
        })
        return this._isEffectMute;
    }

    //背景静音
    private _isBGMMute: boolean = false
    set isBGMMute(value: boolean) {
        this._isBGMMute = value
        StorageManager.saveData("背景静音", value);
    }
    get isBGMMute() {
        StorageManager.getData("背景静音", data => {
            return (this.isBGMMute = data)
        }, () => {
            this.isBGMMute = false
            return this._isBGMMute
        })
        return this._isBGMMute;
    }

    private curBGM: path;


    /**
     * 打开音效
     */
    openEffect() {
        if (this.isEffectMute) {
            this.isEffectMute = false;
        }
    }
    /**
     * 关闭音效
     */
    closeEffect() {
        if (!this.isEffectMute) {
            this.isEffectMute = true;
            cc.audioEngine.stopAllEffects();
        }
    }
    /**
    * 播放音效
    * @param url 文件路径
    */
    playEffect(url: path) {
        if (this.isEffectMute) return;
        cc.resources.load("sounds/" + url, cc.AudioClip, (err: Error, res: cc.AudioClip) => {
            if (err) {
                console.error(err)
            } else {
                cc.audioEngine.playEffect(res, false)
            }
        })
    }
    /**
     * 播放点击音效
     * @param url 文件路径
     */
    playClickEffect() {
        if (this.isEffectMute) return;
        cc.resources.load("sounds/click", cc.AudioClip, (err: Error, res: cc.AudioClip) => {
            if (err) {
                console.error(err)
            } else {
                cc.audioEngine.playEffect(res, false)
            }
        })
    }
    /**
     * 设置音效音量
     */
    setVolumeEffect(value) {
        this.volumeEffect = value
        cc.audioEngine.setMusicVolume(value);
    }


    /**
     * 设置使用该函数
     * 打开背景音乐
     */
    openBGM() {
        if (this.isBGMMute) {
            this.isBGMMute = false;
            if (this.curBGM) {
                this.playBGM(this.curBGM);
            }
        }
    }
    /**
     * 设置使用该函数
     * 关闭背景音乐
     */
    closeBGM() {
        if (!this.isBGMMute) {
            this.isBGMMute = true;
            cc.audioEngine.stopMusic();
        }
    }
    /**
     * 播放背景音乐
     * @param url 文件路径
     */
    playBGM(url: path) {
        this.curBGM = url
        if (this.isBGMMute) return;
        cc.resources.load("sounds/" + url, cc.AudioClip, (err: Error, res: cc.AudioClip) => {
            if (err) {
                console.error(err)
            } else {
                console.log("播放")
                cc.audioEngine.playMusic(res, true)
            }
        })
    }
    /**
     * 恢复背景音乐
     */
    resumeBGM() {
        cc.audioEngine.resumeMusic()
    }
    /**
     * 暂停背景音乐
     */
    pauseBGM() {
        cc.audioEngine.pauseMusic()
    }
    /**
     * 设置背景音乐大小
     * @param value 0-1
     */
    setVolumeBGM(value: number) {
        this.volumeBGM = value
        cc.audioEngine.setMusicVolume(value);
    }
}
export default new SoundsManager();